package com.njlime.member.repository.sms;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.TField;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-7-18.
 */
@Repository
public class FieldDaoImpl extends BaseDaoImpl<TField> {

    /**
     * 获取列表
     *
     * @param pageBean
     * @return
     */
    public PageBean list(PageBean pageBean ,  TField field ) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_filed t ");
        sb.append("left join t_unit u on u.id = t.unit_id ");
        sb.append("where 1=1 ");
        if(field.getStatus() != null && field.getStatus().intValue() != 0){
            sb.append("and t.status = ? ");
            arrList.add(field.getStatus());
        }
        dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "u", "t");
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        pageBean.setTotalPage(pageBean.getTotalPage());
        if (num > 0) {
            sb.append(" order by short_num limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 更新
     *
     * @param info 频道内容
     * @return
     */
    public void update(TField info) {
            StringBuilder sb = new StringBuilder();
            List<Object> arrList = new ArrayList<Object>();
            sb.append("UPDATE t_filed set update_time = now(),updater = ?");
            arrList.add(info.getUpdater());
            sb.append(",short_num = ? ");
             sb.append(" where id= ?  and unit_id = ?");

            arrList.add(info.getShort_num());
            arrList.add(info.getId());
            arrList.add(info.getUnit_id());
            updateJdbc(sb.toString(), arrList);
    }

    /**
     * 更新
     *
     * @param info 频道内容
     * @return
     */
    public int updateStatus(TField info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("UPDATE t_filed set update_time = now(),updater = ?");
        arrList.add(info.getUpdater());
        sb.append(",status = ? ");
        sb.append(" where id= ?  and unit_id = ?");

        arrList.add(info.getStatus());
        arrList.add(info.getId());
        arrList.add(info.getUnit_id());
        return updateJdbc(sb.toString(), arrList);
    }




    public int order(   TField info , int type,int unidId,int userId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();

        sb.append("SELECT " +
                " * " +
                "FROM " +
                " t_filed " +
                "WHERE " +
                " short_num "
        );
        if (type == 1) {
            sb.append(" <");
        } else if (type == 2) {
            sb.append(" > ");
        } else {
            return 0;
        }
        sb.append(" ( " +
                "  SELECT " +
                "   short_num " +
                "  FROM " +
                "   t_filed " +
                "  WHERE " +
                "   id = ?  and unit_id = ?" +
                " ) " +
                "ORDER BY " +
                " short_num ");
        if (type == 1) {
            sb.append(" DESC ");
        }
        sb.append("LIMIT 0,1");
        arrList.add(info.getId());
        arrList.add(unidId);
        List<TField> fieldList =  findList(sb.toString(), arrList);

        if (fieldList.size() > 0) {
            arrList.clear();
            //需要变更的
            TField info1 = fieldList.get(0);
            sb.setLength(0);
            sb.append("UPDATE t_filed set update_time = now(),updater = ?");
            arrList.add(userId);
            sb.append(",short_num = ? ");
            arrList.add(info1.getShort_num());
            sb.append(" where id= ?  and unit_id = ?");
            arrList.add(info.getId());
            arrList.add(unidId);
            updateJdbc(sb.toString(), arrList);
            sb.setLength(0);
            arrList.clear();
            sb.append("UPDATE t_filed set update_time = now(),updater = ?");
            arrList.add(userId);
            sb.append(",short_num = ? ");
            arrList.add(info.getShort_num());
            sb.append(" where id= ?  and unit_id = ?");
            arrList.add(info1.getId());
            arrList.add(unidId);
            updateJdbc(sb.toString(), arrList);
        }

        return 1;
    }
}
